package com.alinesno.cloud.base.logger.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alinesno.cloud.base.logger.entity.LogTriggerConditionEntity;
import com.alinesno.cloud.base.logger.entity.LogTriggerEntity;
import com.alinesno.cloud.base.logger.repository.LogTriggerRepository;
import com.alinesno.cloud.base.logger.service.ILogTriggerConditionService;
import com.alinesno.cloud.base.logger.service.ILogTriggerService;
import com.alinesno.cloud.common.core.service.impl.IBaseServiceImpl;

/**
 * <p> 预警触发 服务实现类 </p>
 *
 * @author WeiXiaoJin
 * @since 2020-01-26 09:30:11
 */
@Service
public class LogTriggerServiceImpl extends IBaseServiceImpl<LogTriggerEntity, String> implements ILogTriggerService {

	//日志记录
	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(LogTriggerServiceImpl.class);

	@Autowired
	private LogTriggerRepository repository ;

	@Autowired
	private ILogTriggerConditionService conditionService ; 

	/**
	 * 保存文档和条件
	 */
	@Override
	public void saveTrigger(LogTriggerEntity dto, String[] conditionName, String[] conditionValue, String[] conditionSpel) {
		repository.save(dto) ;
		
		// 保存条件
		List<LogTriggerConditionEntity> cList = new ArrayList<LogTriggerConditionEntity>() ; 
		for(int i = 0 ; i < conditionName.length ; i ++) {
			String n = conditionName[i] ; 
			String v = conditionValue[i] ; 
			String s = conditionSpel[i] ;
			
			LogTriggerConditionEntity c = new LogTriggerConditionEntity() ; 
			c.setConditionName(n);
			c.setConditionValue(v);
			c.setConditionSpel(s);
			c.setTriggerId(dto.getId());
			
			cList.add(c) ; 
		}
		
		conditionService.saveAll(cList); 
	}

}
